﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.Services;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders.Logic;
using Shop.Web.AppCode;

namespace Shop.Web.Controls
{
    public partial class Catalog : System.Web.UI.UserControl
    {
        private ICollection<ProductGroup> ds;
        private bool allowEdit = false;

        public ICollection<ProductGroup> DataSource
        {
            get { return ds; }
            set { ds = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Customer customer = Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null
                                        ? Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object
                                        : null;
                if (customer != null && customer.Role == CustomerRole.Admin)
                {
                    allowEdit = true;
                }
                rprMain.DataSource = DataSource;
                rprMain.DataBind();
            }

        }

        protected void rprMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ProductGroup item = (ProductGroup)e.Item.DataItem;
            if (allowEdit)
            {
                HtmlAnchor editLink = (HtmlAnchor) e.Item.FindControl("editLink");
                if (editLink != null)
                {
                    editLink.Visible = true;
                    editLink.HRef = "~/ProductDetails.aspx?action=edit&id=" + item.Id;
                }
                Literal ltInvisible = (Literal) e.Item.FindControl("ltInvisible");
                if (ltInvisible != null)
                {
                    ltInvisible.Visible = !item.Visible;
                }
            }
            HtmlImage imgProduct = (HtmlImage)e.Item.FindControl("imgProduct");
            if (imgProduct != null)
            {
                imgProduct.Alt = item.Name;
                if (File.Exists( HttpContext.Current.Server.MapPath("images") + "\\Product\\" + string.Format("thumb_IMG{0}.jpg", item.Id)))
                {
                    imgProduct.Attributes["src"] = "~/images/Product/thumb_IMG" + item.Id.ToString() + ".jpg";
                    if (File.Exists(HttpContext.Current.Server.MapPath("images") + "\\Product\\" + string.Format("IMG{0}.jpg", item.Id)))
                    {
                        imgProduct.Attributes["class"] = "wrap ui-corner-all";
                        imgProduct.Attributes["longdesc"] = "images/Product/IMG" + item.Id.ToString() + ".jpg";
                    }
                }
                else
                {
                    imgProduct.Attributes["src"] = ShopBasePage.NoImage;
                    imgProduct.Attributes["class"] = "ui-corner-all";
                }

            }
        }
    }
}